package com.usernet.product.server;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;

import com.usernet.product.dao.CommentDao;
import com.usernet.product.dao.ErrorDao;
import com.usernet.product.entity.Comment;
import com.usernet.product.entity.Error;
import com.usernet.product.entity.Software;
import com.usernet.product.entity.Users;

public class ErrorService {

	private ErrorDao errorDao = new ErrorDao();
	private CommentDao commentDao = new CommentDao();

	/**
	 * 报错
	 */
	public String logcat(HttpServletRequest request) {
		String ret = null;
		try {
			String softId = request.getParameter("softId");
			String message = request.getParameter("message");
			String userId = request.getParameter("userId");

			Error error = new Error();
			error.setInfo(message);
			error.setStatus(0);
			error.setTime(new Timestamp(System.currentTimeMillis()));
			Users user = null;
			if (userId != null) {
				user = new Users();
				user.setId(Integer.parseInt(userId));
			}
			Software software = new Software();
			software.setId(Integer.parseInt(softId));
			error.setUsers(user);
			error.setSoftware(software);

			int code = errorDao.save(error);
			if (code > 0) {
				ret = MarketUtils.getResJson("[]");
			} else {
				ret = MarketUtils.getErrorJson("报错失败");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			ret = MarketUtils.getThrowJson();
		}
		return ret;
	}

	/**
	 * 评分，评论
	 */
	public String comment(HttpServletRequest request) {
		String ret = null;
		try {
			String softId = request.getParameter("softId");
			String message = request.getParameter("message");
			String userId = request.getParameter("userId");
			String avg = request.getParameter("avg");
			Comment comment = new Comment();
			comment.setInfo(message);
			comment.setScore(Integer.parseInt(avg));
			comment.setStatus(0);
			comment.setTime(new Timestamp(System.currentTimeMillis()));
			Users user = null;
			if (userId != null) {
				user = new Users();
				user.setId(Integer.parseInt(userId));
			}
			Software software = new Software();
			software.setId(Integer.parseInt(softId));
			comment.setSoftware(software);

			int code = commentDao.save(comment);
			if (code > 0) {
				ret = MarketUtils.getResJson("[]");
			} else {
				ret = MarketUtils.getErrorJson("评分失败");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			ret = MarketUtils.getThrowJson();
		}
		return ret;
	}
}
